using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The DepartmentTableDB class is responsible for interacting with the database to retrieve and store information
    /// about DepartmentTable objects.
    /// </summary>
    public class DepartmentTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of DepartmentTable from the underlying datasource.
        /// </summary>
        /// <param name="schoolKey">The unique SchoolKey of the DepartmentTable in the database.</param>
        /// <returns>An DepartmentTable when the SchoolKey was found in the database, or null otherwise.</returns>
        public static DepartmentTable GetItem(int schoolKey, int LocationKey)
        {
            DepartmentTable myDepartmentTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDepartmentTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);
                    myCommand.Parameters.AddWithValue("@LocationKey", LocationKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myDepartmentTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myDepartmentTable;
            }
        }

        /// <summary>
        /// Returns a list with DepartmentTable objects.
        /// </summary>
        /// <returns>A generics List with the DepartmentTable objects.</returns>
        public static DepartmentTableList GetList(int SchoolKey, int LocationKey)
        {
            DepartmentTableList tempList = new DepartmentTableList();

            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDepartmentTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myCommand.Parameters.AddWithValue("@LocationKey", LocationKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a DepartmentTable in the database.
        /// </summary>
        /// <param name="myDepartmentTable">The DepartmentTable instance to save.</param>
        /// <returns>The new SchoolKey if the DepartmentTable is new in the database or the existing SchoolKey when an item was updated.</returns>
        public static int Save(DepartmentTable myDepartmentTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDepartmentTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myDepartmentTable.DepartmentKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@departmentKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@departmentKey", myDepartmentTable.DepartmentKey);
                    }

                    if (myDepartmentTable.SchoolKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myDepartmentTable.SchoolKey);
                    }

                    if (myDepartmentTable.LocationKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@LocationKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@LocationKey", myDepartmentTable.LocationKey);
                    }

                    if (string.IsNullOrEmpty(myDepartmentTable.DepartmentCode))
                    {
                        myCommand.Parameters.AddWithValue("@departmentCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@departmentCode", myDepartmentTable.DepartmentCode);
                    }

                    if (string.IsNullOrEmpty(myDepartmentTable.DepartmentName))
                    {
                        myCommand.Parameters.AddWithValue("@departmentName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@departmentName", myDepartmentTable.DepartmentName);
                    }

                    if (string.IsNullOrEmpty(myDepartmentTable.DepartmentDescription))
                    {
                        myCommand.Parameters.AddWithValue("@departmentDescription", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@departmentDescription", myDepartmentTable.DepartmentDescription);
                    }

                    if (string.IsNullOrEmpty(myDepartmentTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myDepartmentTable.Remark);
                    }

                    myCommand.Parameters.AddWithValue("@status", myDepartmentTable.Status);
                    //if (myDepartmentTable.Status = false)
                    //{
                    //    myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@status", myDepartmentTable.Status);
                    //}

                    //if (myDepartmentTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myDepartmentTable.CreatedDateTime);
                    //}
                    //if (myDepartmentTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myDepartmentTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a DepartmentTable from the database.
        /// </summary>
        /// <param name="schoolKey">The SchoolKey of the DepartmentTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int departmentKey, int schoolKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDepartmentTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@SchoolKey", schoolKey);
                    myCommand.Parameters.AddWithValue("@DepartmentKey", departmentKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the DepartmentTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static DepartmentTable FillDataRecord(IDataRecord myDataRecord)
        {
            DepartmentTable myDepartmentTable = new DepartmentTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DepartmentKey")))
            {
                myDepartmentTable.DepartmentKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("DepartmentKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myDepartmentTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
            {
                myDepartmentTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DepartmentCode")))
            {
                myDepartmentTable.DepartmentCode = myDataRecord.GetString(myDataRecord.GetOrdinal("DepartmentCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DepartmentName")))
            {
                myDepartmentTable.DepartmentName = myDataRecord.GetString(myDataRecord.GetOrdinal("DepartmentName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DepartmentDescription")))
            {
                myDepartmentTable.DepartmentDescription = myDataRecord.GetString(myDataRecord.GetOrdinal("DepartmentDescription"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myDepartmentTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myDepartmentTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myDepartmentTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myDepartmentTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myDepartmentTable;
        }
    }

}
